package cn.mugutu.finance.controller;

import cn.mugutu.finance.annotation.SysLog;
import cn.mugutu.finance.annotation.SysResource;
import cn.mugutu.finance.domain.system.Resource;
import cn.mugutu.finance.domain.system.Role;
import cn.mugutu.finance.dto.BaseResult;
import cn.mugutu.finance.dto.BaseResult.EasyUIResult;
import cn.mugutu.finance.dto.RoleDto;
import cn.mugutu.finance.exception.BusinessException;
import cn.mugutu.finance.vo.RoleAddReqVO;
import cn.mugutu.finance.query.RoleQuery;
import cn.mugutu.finance.service.system.RoleService;
import cn.mugutu.finance.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/listUI")
    @SysResource(name = "角色管理", code = "role:listui")
    public String listUI() {
        return "role/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public EasyUIResult<RoleDto> list(RoleQuery query, int page, int rows) {
        setEasyUIParamsInQuery(query, page, rows);
        Pager<Role> pager = roleService.findByPage(query);
        List<RoleDto> dtoList = RoleDto.roleListToRoleDtoList(pager.getRows());
        return new EasyUIResult<RoleDto>(pager.getTotalCount(), dtoList);
    }

    @RequestMapping("/get")
    @ResponseBody
    public Role getById(Long id) {
        return roleService.get(id);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @SysLog(description = "添加/修改角色")
    public BaseResult saveOrUpdate(RoleAddReqVO vo) {
        return roleService.saveOrUpdateRole(vo);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @SysLog(description = "删除角色")
    public BaseResult delete(Long id) {
        try {
            roleService.delete(id);
            return new BaseResult(true, "删除成功");
        } catch (Exception e) {
            throw new BusinessException("删除失败,改角色已经赋予给了某用户或其他情况,或者联系系统管理员处理." + e.getMessage());
        }
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/all")
    @ResponseBody
    public List<RoleDto> all() {
        List<Role> all = roleService.findAll();
        List<RoleDto> dtoList = RoleDto.roleListToRoleDtoList(all);
        return dtoList;
    }

    /**
     * 查询指定角色所拥有的资源(如果角色Id没有传入,返回的拥有权限就为一个空的集合对象)
     * @param roleId
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/findResourcesByRoleId")
    @ResponseBody
    public EasyUIResult<Resource> findResourceByRoleId(Long roleId, int page, int rows) {
        Pager<Resource> pager = roleService.findResourceByRoleId(roleId, page, rows);
        return new EasyUIResult<>(pager.getTotalCount(), pager.getRows());
    }

    /**
     * 查询指定角色没有拥有的资源(如果角色Id不传入就返回全部资源)
     * @return
     */
    @RequestMapping("/findResourcesByRoleIdNegation")
    @ResponseBody
    public EasyUIResult<Resource> findResourceByRoleIdNegation(Long roleId, int page, int rows) {
        Pager<Resource> pager = roleService.findResourceByRoleIdNegation(roleId, page, rows);
        return new EasyUIResult<>(pager.getTotalCount(), pager.getRows());
    }

    /**
     * 为某角色去除某权限
     * @param roleId
     * @param resourceId
     * @return
     */
    @RequestMapping("/moveOutResource")
    @ResponseBody
    public BaseResult moveOutResource(Long roleId, Long resourceId) {
        return roleService.moveOutResource(roleId, resourceId);
    }

    /**
     * 为某角色添加权限
     * @param roleId
     * @param resourceId
     * @return
     */
    @RequestMapping("/moveInResource")
    @ResponseBody
    public BaseResult moveInResource(Long roleId, Long resourceId) {
        return roleService.moveInResource(roleId, resourceId);
    }

}
